package com.lyon.custom.core;

import cn.hutool.core.bean.BeanUtil;
import com.lyon.custom.config.LyonConfigProperties;

import java.util.Map;
import java.util.Properties;

/**
 * @author LeeYan9
 * @since 2021-11-26
 */
public class LyonConfigManager {

    private static volatile ConfigService configService;
    private LyonConfigProperties lyonConfigProperties;

    public LyonConfigManager(LyonConfigProperties lyonConfigProperties) {
        this.lyonConfigProperties = lyonConfigProperties;
    }

    public ConfigService getConfigService() {
        if (configService == null) {
            configService = createConfigService(lyonConfigProperties);
        }
        return configService;
    }

    private ConfigService createConfigService(LyonConfigProperties lyonConfigProperties) {
        synchronized (LyonConfigManager.class) {
            if (configService == null) {
                Properties properties = new Properties();
                Map<String, Object> map = BeanUtil.beanToMap(lyonConfigProperties);
                properties.putAll(map);
                return new LyonConfigServiceImpl(properties);
            }
        }
        return configService;
    }

    public LyonConfigProperties getConfigProperties() {
        return this.lyonConfigProperties;
    }
}
